<?php
/**
 * User: Titan
 * Date: Sep 28, 2010
 * Time: 6:41:22 PM
 */

/**
 * install_launch()
 * 
 * Send the user to the installer page
 * 
 * @param mixed $path
 * @return void
 */
function install_launch($path)
{
	global $base_url;
	header('Location: ' . $base_url . '/' . $path);
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: post-check=0, pre-check=0', FALSE);
	header('Pragma: no-cache'); 
	exit;
}

/**
 * install_write_settings()
 * 
 * Writes the settings to a settings.inc file
 * 
 * @param mixed $settings
 * @return void
 */
function install_write_settings($settings = array())
{
	$keys = array();
	foreach ($settings as $setting => $data) 
	{
		$GLOBALS[$setting] = $data;
		$keys[] = $setting;
	}
	
	$buffer = NULL;
	$first = TRUE;
	if ($fp = fopen('./default.settings.inc', 'r'))
	{
		while (!feof($fp))
		{
			$line = fgets($fp);
			if ($first && substr($line, 0, 5) != '<?php')
			{
				$buffer = "<?php\n\n";
			}
			$first = FALSE;
			
			if (substr($line, 0, 1) == '$')
			{
				preg_match('/\$([^ ]*) /', $line, $variable);
				if (in_array($variable[1], $keys))
				{
					// this becomes $setting = 'value';
					$setting = $settings[$variable[1]];
					$buffer .= '$' . $variable[1] . " = '" . $setting . "';\n";
					unset($settings[$variable[1]]);
				}
				else
				{
					// comment or explanation 
					$buffer .= $line;
				}
			}
			else
			{
				$buffer .= $line;
			}
		}
		
		fclose($fp);
		
		$fp = @fopen('./settings.inc', 'w');
		if (!$fp || fwrite($fp, $buffer) === FALSE)
		{
			system_set_message('Failed to write to the settings.inc file. Please verify the file permissions.');
			
			return FALSE;
		}
		
		return TRUE;
	}
	else
	{
		system_set_message('Failed to open the default.settings.inc file. Please verify the file permissions.');
		
		return FALSE;
	}
}

/**
 * install_schema()
 * 
 * Installs the database schema
 * 
 * @return void
 */
function install_schema()
{
	global $db_schema;
	if (isset($db_schema) && $db_schema !== FALSE)
	{
		db_query("DROP DATABASE IF EXISTS %s", $db_schema);
		$result = db_query("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", $db_schema);
		
		if ($result !== FALSE)
		{
			mysql_select_db($db_schema);
			system_set_message('The <code>' . $db_schema . '</code> database was successfully created.');
			
			return TRUE;
		}
		else
		{
			system_set_message('The <code>' . $db_schema . '</code> database was not created.');
			
			return FALSE;
		}
	}
}

/**
 * install_system()
 * 
 * Install all the databases that the system consists of
 * 
 * @return void
 */
function install_system()
{
	$result = install_schema();
	
	$result = $result && Feature::install();
	$result = $result && Milestone::install();
	$result = $result && Developer::install();
	$result = $result && Task::install();
	
	return $result;
}
